package com.ruoyi.lvlian.util;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class OrderNumberGenerator {

    private static final int ORDER_NUMBER_LENGTH = 13;
    private static final Random RANDOM = new Random();

    // 生成跟单号的方法
    public static String generateOrderNumber() {
        // 获取当前时间戳部分，格式为 yyyyMMddHHmm
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmm");
        String timestamp = sdf.format(new Date());

        // 计算需要的随机数部分的长度
        int randomLength = ORDER_NUMBER_LENGTH - timestamp.length();

        // 生成随机数部分
        StringBuilder randomPart = new StringBuilder();
        for (int i = 0; i < randomLength; i++) {
            randomPart.append(RANDOM.nextInt(10)); // 生成 0-9 的随机数
        }

        // 组合时间戳和随机数部分
        return timestamp + randomPart.toString();
    }

    public static void main(String[] args) {


        String jsonResponse = "{ \"code\": 500, \"msg\": \"{\\\"errcode\\\":40001,\\\"errmsg\\\":\\\"invalid credential, access_token is invalid or not latest, could get access_token by getStableAccessToken, more details at https://mmbizurl.cn/s/JtxxFh33r rid: 666a9bb6-30854cdd-73ebbd4f\\\"}\", \"data\": null }";

        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode rootNode = objectMapper.readTree(jsonResponse);
            JsonNode msgNode = rootNode.path("msg");

            // 去掉转义字符
            String msg = msgNode.asText();
            JsonNode msgContentNode = objectMapper.readTree(msg);
            String prettyMsg = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(msgContentNode);

            System.out.println("Formatted message: ");
            System.out.println(prettyMsg);
        } catch (Exception e) {
            e.printStackTrace();
        }



        String phone="[\"15723122196\"]";
        phone=phone.replaceAll("\"","").replaceAll("\\[","") .replaceAll("\\]","");
        System.out.println( phone);
        for (int i = 0; i < 10; i++) {
            String orderNumber = generateOrderNumber();
            System.out.println("Generated Order Number: " + orderNumber);
        }
    }
}
